import requests
# 引入解析html包
from lxml import etree

url = 'https://qq.131439.xyz/book/douluodalu1/1.html'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
}



openFlag = True
# 实现获取当前网页中的下一章功能，这样就能获取所有章节内容；
while openFlag:

    # 请求
    response = requests.get(url=url, headers=headers)
    response.encoding = 'utf-8'
    text = response.text
    # print(body_text)
    # 解析html
    etree_text = etree.HTML(text)
    # xpahth解析html
    # 获取章节标题
    body_text_title = etree_text.xpath('//div[@class="entry-tit"]/h1/text()')[0]
    # 获取当前章节内容
    body_text = etree_text.xpath('//div[@class="m-post"]/p/text()')
    body_text = '\n'.join(body_text)  # 将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串
    # 获取下一章节链接
    body_next_url = etree_text.xpath('//table[@class="wenxue2"][1]//td[2]/a/@href')[0]
    print(body_text_title)
    # print(body_text)
    url = 'https://qq.131439.xyz' + body_next_url
    print(url)

    # 判断是否为最后一章节
    if '/book/douluodalu1/' == body_next_url :
        print('最后一张结束while...')
        openFlag = False



